<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/testTpl1.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <!-- InstanceBeginEditable name="doctitle" -->
    <title>TestPage</title>
    <!-- InstanceEndEditable -->
    <!-- InstanceBeginEditable name="head" -->
    <!-- InstanceEndEditable -->
    <link rel="stylesheet" type="text/css" href="../style/google/KitchenSink.css">
    <!-- InstanceBeginEditable name="additionalStyle" -->
    <!-- InstanceEndEditable -->

    <script type="text/javascript">
    var djConfig = {
        //isDebug: true
        //,debugAtAllCosts: true
    };

var lotex = {};

// OptionBuilder.withLongOpt( "sheet" ) .withDescription( "表示这是一个工作表，可选参数：工作表名称。如 –s sheet1" ).hasOptionalArg(“name”) .create(“s”) ;
// cli = "Xx1 –s sheet1 xx2 –mo 1 2 xx3";

(function(){
lotex.cli = {};
var cli = lotex.cli;

cli.Option = function() {
    this.prefix = '-';
    this.longPrefix = '--';
    this.valueSeparator = ' ';
    this.args = ['arg'];
};

cli.OptionBuilder = function(){
    var option;
    var initFlag = true;

    function init() {
        if(initFlag) option = new cli.Option();
        initFlag = false;
    };
    init();

    return {
        withLongOpt: function(longOpt) {
            init();
            option["longOpt"] = longOpt;
            return this;
        },
        create: function(opt) {
            init();
            initFlag = true;

            option["opt"] = opt;
            return option;
        }
    };
}();

cli.Options = function(){
    this.options = [];
};

cli.Options.prototype = {
    addOption: function(option) {
        this.options.push(option);
    },
    getOption: function(optname) {
        var opts = this.options;
        for(var i=0;i<opts.length;i++) {
            if(optname==opts[i]['longOpt'] ||
                optname==opts[i]['opt'])
                return opts[i];
        }
        return null;
    }
}


cli.Parser = {
    parse: function(options, args){
        return {
            foo: function(){alert(args);},
            getOptionValue: function(optname) {
                var opt = options.getOption(optname);

                var shortOpt = opt['prefix'] + opt['opt'];
                var langOpt = opt['longPrefix'] + opt['longOpt'];
                var res = '('+shortOpt+'|'+langOpt+')(?:$|\\s+)(.*)';

                var match = args.match(new RegExp(res, ''));
                if (match == null) return null;
                // TODO Array.split(Regexp)

                var args1 = match[2].split(' ');
                args1.splice(opt['args'].length, args1.length);
                return args1;
            }
        };
    }
}

var OB = cli.OptionBuilder;

var sheet = OB.withLongOpt("sheet").create("s");
var ver = OB.withLongOpt("version").create("v");

var options = new cli.Options();
options.addOption(sheet);
options.addOption(ver);

var args = 'Xx1 -s sheet1 xx2 --version 3.14159 2 xx3';
var cmd = cli.Parser.parse(options, args);
alert(cmd.getOptionValue('s')[0]);
alert(cmd.getOptionValue('version')[0]);
})();

    </script>

    <!-- InstanceBeginEditable name="dojoRequire" -->

    <script type="text/javascript">
    </script>

    <!-- InstanceEndEditable -->
    <!-- InstanceBeginEditable name="headScript" -->

    <script type="text/javascript">
    </script>

    <!-- InstanceEndEditable -->
</head>
<body>
    <table style="width: 100%" cellspacing="0" cellpadding="0">
        <tbody>
            <tr>
                <td style="vertical-align: top" align="left">
                    <div class="ks-Info">
                        Testpage</div>
                </td>
            </tr>
            <tr>
                <td style="vertical-align: top" align="left">
                    <table class="ks-Sink" cellspacing="0" cellpadding="0">
                        <tbody>
                            <tr>
                                <td style="vertical-align: top; width: 100%; height: 100%" align="left">
                                    <div id="container">
                                        <!-- InstanceBeginEditable name="htmlContainer" -->
                                        <!-- InstanceEndEditable -->
                                    </div>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </td>
            </tr>
        </tbody>
    </table>
    <fieldset>
        <legend>Output</legend>
        <div id="debug.output">
        </div>
    </fieldset>
</body>
<!-- InstanceEnd --></html>
